/*
 * BTNC1.c
 *
 *  Created on: 17/11/2012
 *      Author: Richard
 */

#include <string.h>
#include "BTNC1.h"

#include "../LED/LEDC1.h"
#include "../../SENSORS/SNSR/SNSRC1.h"

#include "../../../sensor.h"
#include "../../../Driver/RTC/RTCC1.h"
#include "../../../Driver/HWTIMER/HWTIMERC1.h"

/* module to check for key presses (long/short) */

#define BTN_nMaxPressTime     nu16Bit13

#define BTN_nButtonPressMask  nu16Bit0
#define BTN_nLongPressMask    nu16Bit1
#define BTN_nShortPressMask   nu16Bit2

typedef union
{
   uint16 u16Data;
   struct
   {
      uint16 biKeyPressed :1;
      uint16 biLongPress  :1;
      uint16 biShortPress :1;
      uint16 u16Timer     :13;
   } Bits;
} BTN__tunButtonData;

static BTN__tunButtonData  BTN__stButtonData;
static SWTIM_tstTimer      BTN__stSampleTimer;

/* Store local time */
static volatile RTC_tstTime BTN__stTime;

/* Prototypes */
void BTN__vIncrementTimer( SWTIM_tstTimer *pstTimer );
void BTN__vGetTime( RTC_tstTime *stTime );

void BTN_vReset( void )
{
   memset( (void*)&BTN__stButtonData, 0, sizeof(BTN__stButtonData) );

   BTN__stSampleTimer.interval = BTN_nSampleTime;
   BTN__stSampleTimer.mode = SWTIM_nenPeriodicMode;
   BTN__stSampleTimer.handler = &BTN__vIncrementTimer;
}

void BTN__vIncrementTimer( SWTIM_tstTimer *pstTimer )
{
   (void)pstTimer;

   if( BTN__stButtonData.Bits.biKeyPressed == True )
   {
      BTN__stButtonData.Bits.u16Timer += BTN_nSampleTime;
      if( BTN__stButtonData.Bits.u16Timer >= BTN_nLongPress )
      {
         /* long press */
         /* bit mask, clears button press flag and timer value */
         BTN__stButtonData.u16Data = BTN_nLongPressMask;
         APP_unRequests.F.biButtonPress = 1;
      }
   }
}

void BTN_vPressEvent( DEB_tstDebounceEvent * pstEventInfo )
{
   /* pressed */
   if( pstEventInfo->enInput == DEB_nenButton )
   {
      if( pstEventInfo->enLogicLevel == tHi )
      {
         if( BTN__stButtonData.Bits.biKeyPressed == False )
         {
            /* bit mask, clears button press flag and timer value */
            BTN__stButtonData.u16Data = BTN_nButtonPressMask;
            APP_unRequests.F.biButtonPress = 1;

            /* start timer */
            SWTIM_vTimerStart( &BTN__stSampleTimer );
         }
      }
      /* released */
      else
      {
         if( BTN__stButtonData.Bits.biKeyPressed == True )
         {
            if( BTN__stButtonData.Bits.u16Timer < 1000 )
            {
               /* short press */
               /* bit mask, clears button press flag and timer value */
               BTN__stButtonData.u16Data = BTN_nShortPressMask;
               APP_unRequests.F.biButtonPress = 1;
            }
         }

         /* stop timer */
         SWTIM_vTimerStop( &BTN__stSampleTimer );
      }
   }
}

bool BTN_boProcessEvent( void )
{
   if( BTN__stButtonData.Bits.biShortPress == True )
   {
      /* on each button press, cycle through each of the LED display modes */
      switch( LED_enGetCurrentOperation() )
      {
         case LED_nenOff:
         case LED_nenWaterLevel:
            LED_vSetOutput( LED_nenFlash );
            break;
         case LED_nenFlash:
            LED_vSetOutput( LED_nenFlashFast );
            break;
         case LED_nenFlashFast:
            LED_vSetOutput( LED_nenWaterLevel );
            break;
         default:
            break;
      }
   }
   else if( BTN__stButtonData.Bits.biLongPress == True )
   {
      LED_vSetOutput( LED_nenOff );
   }

   return True;
}

/* Test get time function - interrupt context */
void BTN__vGetTime( RTC_tstTime *stTime )
{
   /* store local copy of time given from RTC module */
   BTN__stTime = *stTime;
   __no_operation();
}
